package xin.codedream.codelog.web.interceptor;


import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import xin.codedream.codelog.model.entity.User;
import xin.codedream.codelog.model.enums.ConstEnum;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * 登录拦截器
 *
 * @author NGLSL
 * @date 2018/8/4
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        User user = (User) request.getSession().getAttribute(ConstEnum.USER_SESSION_KEY.getValue());
        if (Objects.nonNull(user)) {
            return true;
        }
        response.sendRedirect("/admin/login");
        return false;
    }
}
